﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;
using CIT.WMS.RM.Bll;

namespace CIT.WMS.RM
{
    public partial class FrmRMGroupO : Form
    {

        string billno = string.Empty;
        string rowno = string.Empty;
        string orderno = string.Empty;

        public FrmRMGroupO()
        {
            InitializeComponent();
        }

        private void FrmRMGroupO_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
          
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                bindBillInfo();
            }
        }
        public void bindBillInfo()
        {
            
            try
            {
                var splitList = textBox1.Text.Split('-');
                billno = splitList.FirstOrDefault();
                rowno = splitList[1].ToString();
                orderno = splitList[2].ToString();
            }
            catch 
            {
                MessageBox.Show("请选择正确的单号!");
                return;
            }

            var BillInfoDt = Bll_RMIn.GetBillInfoByCondition(billno, rowno, orderno);
            if (BillInfoDt != null && BillInfoDt.Rows.Count > 0)
            {
                label6.Text = BillInfoDt.Rows[0]["Storage_SN"].ToString();
                label7.Text = BillInfoDt.Rows[0]["RelatedBill"].ToString();
                label8.Text = BillInfoDt.Rows[0]["GroupSum"].ToString();
                label9.Text = BillInfoDt.Rows[0]["FGQty"].ToString();
            }
            else 
            {
                MessageBox.Show("请选择正确的单号!");
                return;
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

            DialogResult dr = MessageBox.Show("分配订单信息[" + textBox1.Text + "]，是否确认？", "入库确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

            if (dr == DialogResult.Cancel)
            {
                return;
            }

            FrmRMGroupS fr = new FrmRMGroupS(billno, rowno, orderno);
            fr.ShowDialog();

            bindBillInfo();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

                DialogResult dr = MessageBox.Show("请确认[单号为：" + billno + "]的采购入库单已分配完成！", "入库确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                var NoGroupDt = Bll_RMIn.GetNoGroupRows(billno);
                if (NoGroupDt != null && NoGroupDt.Rows.Count > 0)
                {
                    MessageBox.Show("该采购单还有未分组完成的行数据。");
                    return;
                }
                else
                {

                    string status = "5";
                    var NoFGDt = Bll_RMIn.GetIsNoFG(billno);
                    if (NoFGDt != null && NoFGDt.Rows.Count > 0)
                    {
                        status = "9";
                    }
                    //修改主表状态

                    bool isSuccess = Bll_RMIn.UpdateGroupStatus(billno, status);
                    if (isSuccess)
                    {
                        MessageBox.Show("操作成功！");
                    }
                }
            }catch(Exception ex)
            {
                MessageBox.Show("数据错误！");
            }
        }
    }
}